package com.eairlv.route.server.service;

import com.alibaba.fastjson.JSON;
import com.eairlv.route.server.entity.Result;
import com.eairlv.route.server.entity.form.AuthenticationForm;
import com.eairlv.route.server.entity.po.AuthenticationPO;
import com.eairlv.route.server.entity.vo.AuthenticationProperties;
import com.eairlv.route.server.entity.vo.AuthenticationVO;
import com.eairlv.route.server.mapper.AuthenticationMapper;
import com.eairlv.route.server.utils.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author eairlv
 * @description
 * @date 14:27 2019/12/11
 */
@Slf4j
@Service
public class AuthenticationService {

    public static final String SESSION_KEY = "paas-route-server:SESSION:";

    @Autowired
    AuthenticationMapper authenticationMapper;

    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 通过session获取认证信息
     * @param session
     * @return
     */
    public Result<AuthenticationVO> session(String session) {
        AuthenticationPO authenticationPO = authenticationMapper.selectById(session);
        if (authenticationPO != null){
            return Result.ok(BeanUtil.copyProperties(authenticationPO, AuthenticationVO.class));
        } else {
            return Result.failMessage("invalid session");
        }
    }

    /**
     * 添加认证信息
     * @param authenticationForm
     * @return
     */
    public Result authentication(AuthenticationForm authenticationForm) {
        AuthenticationPO authenticationPO = BeanUtil.copyProperties(authenticationForm, AuthenticationPO.class);
        authenticationPO.setCreateTime(System.currentTimeMillis());
        authenticationMapper.insert(authenticationPO);
        redisTemplate.opsForValue().set(SESSION_KEY + authenticationForm.getSession(), JSON.toJSONString(BeanUtil.copyProperties(authenticationPO, AuthenticationProperties.class)));
        return Result.ok();
    }

    /**
     * 修改认证信息
     * @param authenticationForm
     * @return
     */
    public Result updateAuthentication(AuthenticationForm authenticationForm) {
        AuthenticationPO authenticationPO = authenticationMapper.selectById(authenticationForm.getSession());
        if (authenticationPO != null){
            BeanUtil.copyProperties(authenticationForm, authenticationPO);
            authenticationMapper.updateById(authenticationPO);
            redisTemplate.opsForValue().set(SESSION_KEY + authenticationForm.getSession(), JSON.toJSONString(BeanUtil.copyProperties(authenticationPO, AuthenticationProperties.class)));
            return Result.ok();
        } else {
            return Result.failMessage("invalid authentication");
        }
    }

    /**
     * 加载所有路由认证
     */
    public void loadAuthentication() {
        List<AuthenticationPO> authenticationPOS = authenticationMapper.selectList(null);
        authenticationPOS.forEach(e -> redisTemplate.opsForValue().set(SESSION_KEY + e.getSession(), JSON.toJSONString(BeanUtil.copyProperties(e, AuthenticationProperties.class))));
    }
}
